﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;

namespace GaiNo.Core.Extensions
{
    public static class CollectionExtensions
    {
        public static string GetStringOrNull(this NameValueCollection nvc, string name)
        {
            return nvc[name];
        }

        public static string GetString(this NameValueCollection nvc, string name)
        {
            return GetStringOrNull(nvc, name) ?? String.Empty;
        }

        public static int? GetIntOrNull(this NameValueCollection nvc, string name)
        {
            int value = 0;
            if (int.TryParse(nvc[name], out value))
                return value;
            return null;
        }

        public static int GetInt(this NameValueCollection nvc, string name)
        {
            return GetIntOrNull(nvc, name).GetValueOrDefault();
        }
        public static short? GetShortOrNull(this NameValueCollection nvc, string name)
        {
            short value = 0;
            if (short.TryParse(nvc[name], out value))
                return value;
            return null;
        }

        public static short GetShort(this NameValueCollection nvc, string name)
        {
            return GetShortOrNull(nvc, name).GetValueOrDefault();
        }
        public static decimal? GetDecimalOrNull(this NameValueCollection nvc, string name)
        {
            decimal value = 0;
            if (decimal.TryParse(nvc[name], out value))
                return value;
            return null;
        }

        public static decimal GetDecimal(this NameValueCollection nvc, string name)
        {
            return GetDecimalOrNull(nvc, name).GetValueOrDefault();
        }

        public static DateTime? GetDateOrNull(this NameValueCollection nvc, string name)
        {
            DateTime value = DateTime.MinValue;
            if (DateTime.TryParse(nvc[name], out value))
                return value;

            return null;
        }

        public static DateTime GetDate(this NameValueCollection nvc, string name)
        {
            return GetDateOrNull(nvc, name).GetValueOrDefault();
        }

        public static T? GetEnumOrNull<T>(this NameValueCollection nvc, string name)
            where T : struct
        {
            var val = GetIntOrNull(nvc, name);
            var enumName = nvc[name];
            if (val.HasValue)
            {
                enumName = Enum.GetName(typeof(T), val);
                return (T)Enum.Parse(typeof(T), enumName, true);
            }

            return default(T);
        }

        public static T GetEnum<T>(this NameValueCollection nvc, string name)
            where T : struct
        {
            var val = GetInt(nvc, name);
            var enumName = nvc[name];
            enumName = Enum.GetName(typeof(T), val);
            if (enumName.IsNullOrEmpty())
                enumName = "0";
            return (T)Enum.Parse(typeof(T), enumName, true);
        }

        public static T GetEnumByName<T>(this NameValueCollection nvc, string name)
            where T : struct
        {
            var val = GetString(nvc, name);
            Array values = System.Enum.GetValues(typeof(T));
            foreach (object value in values)
            {
                if (value.ToString().ToUpper().Equals(val.ToUpper()))
                    return (T)value;
            }
            val = "0";
            return (T)Enum.Parse(typeof(T), val, true);
        }

        public static bool? GetBooleanOrNull(this NameValueCollection nvc, string name)
        {
            var val = GetStringOrNull(nvc, name);
            if (val != null)
            {
                var rst = false;
                bool.TryParse(val, out rst);

                return rst;
            }

            return null;
        }

        public static bool GetBoolean(this NameValueCollection nvc, string name)
        {
            return GetBooleanOrNull(nvc, name).GetValueOrDefault();
        }

        public static NameValueCollection RemoveItem(this NameValueCollection nvc, string name)
        {
            var newNvc = new NameValueCollection(nvc);
            newNvc.Remove(name);

            return newNvc;
        }

        public static NameValueCollection Revert(this NameValueCollection nvc)
        {
            var newNvc = new NameValueCollection();
            for (int i = 0; i < nvc.Count; i++)
            {
                var key = nvc.Keys[i];
                var val = nvc[i];

                newNvc[val] = key;
            }

            return newNvc;
        }
    }
}
